/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.risk.control.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.common.constant.enums.RiskSysDictEnum;
import com.hopes.bizz.risk.control.dto.RiskObjectExcelDTO;
import com.hopes.bizz.risk.control.entity.RiskObjectEntity;
import com.hopes.bizz.risk.control.mapper.RiskObjectMapper;
import com.hopes.bizz.risk.control.service.RiskObjectService;
import com.hopes.bizz.risk.control.vo.RiskObjectExcelVO;
import com.hopes.bizz.risk.parkgis.vo.OrgLevelVO;
import com.hopes.bizz.risk.parkgis.vo.RiskLevelVO;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 风险分析对象
 *
 * @author daijie
 * @date 2023-07-17 17:01:16
 */
@Service
public class RiskObjectServiceImpl extends ServiceImpl<RiskObjectMapper, RiskObjectEntity> implements RiskObjectService {

    @Override
    public ValidateImportBO<RiskObjectExcelDTO> validateImport(List<RiskObjectExcelDTO> excelDTOList) {
        ValidateImportBO<RiskObjectExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<RiskObjectExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        List<RiskObjectEntity> list = this.list();
        for (RiskObjectExcelDTO excelDTO : excelDTOList) {
            // 检验风险对象名称是否存在
            if (list.stream().anyMatch(item -> item.getHazardName().equals(excelDTO.getHazardName()))) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("风险对象名称不允许重复!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getHazardType())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("风险对象类型不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getHazardCode())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("风险对象编码不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getReportDataFlag())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("数据是否上报不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getDeptId())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("责任部门不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getPrincipal())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("责任人不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            } else if (StringUtils.isBlank(excelDTO.getHazardSourceFlag())) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add("是否重大危险源不允许为空!" + "  " + excelDTO.getHazardName());
                }}));
                failedNum++;
                continue;
            }

            saveList.add(excelDTO);
            successNum++;
        }
        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public RiskObjectEntity importDtoToEntity(RiskObjectExcelDTO excelDto) {
        RiskObjectEntity entity= new RiskObjectEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StringUtils.isNotBlank(excelDto.getHazardType())) {
            entity.setHazardType(Integer.valueOf(DictResolver.getItemValueByLabel(RiskSysDictEnum.risk_object_type, excelDto.getHazardType())));
        }
        if (StringUtils.isNotBlank(excelDto.getReportDataFlag())) {
            entity.setReportDataFlag(StringUtils.getUseFlag(excelDto.getReportDataFlag()));
        }
        if (StringUtils.isNotBlank(excelDto.getRiskLevel())) {
            entity.setRiskLevel(Integer.valueOf(DictResolver.getItemValueByLabel(RiskSysDictEnum.risk_level, excelDto.getRiskLevel())));
        }
        if (StringUtils.isNotBlank(excelDto.getHazardSourceFlag())) {
            entity.setHazardSourceFlag(StringUtils.getUseFlag(excelDto.getHazardSourceFlag()));
        }
        if (StringUtils.isNotBlank(excelDto.getHazardSourceLevel())) {
            entity.setHazardSourceLevel(Integer.valueOf(DictResolver.getItemValueByLabel(RiskSysDictEnum.hazard_source_level, excelDto.getHazardSourceLevel())));
        }
        if (StringUtils.isNotBlank(excelDto.getControlLevel())) {
            entity.setControlLevel(DictResolver.getItemValueByLabel(RiskSysDictEnum.risk_control_level, excelDto.getControlLevel()));
        }

        if(StringUtils.isNotBlank(excelDto.getDeptId())) {
            entity.setDeptId(Arrays.stream(excelDto.getDeptId().split(StringUtils.COMMA))
                    .map(it -> DictResolver.getItemValueByLabel(AdminDictEnum.sys_dept, it)).collect(Collectors.joining(",")));
        }
        if(StringUtils.isNotBlank(excelDto.getPrincipal())) {
            entity.setPrincipal(Arrays.stream(excelDto.getPrincipal().split(StringUtils.COMMA))
                    .map(it -> DictResolver.getItemValueByLabel(AdminDictEnum.sys_user, it)).collect(Collectors.joining(",")));
        }
        if(StringUtils.isNotBlank(excelDto.getMainUserId())) {
            entity.setMainUserId(Arrays.stream(excelDto.getMainUserId().split(StringUtils.COMMA))
                    .map(it -> DictResolver.getItemValueByLabel(AdminDictEnum.sys_user, it)).collect(Collectors.joining(",")));
        }
        if(StringUtils.isNotBlank(excelDto.getTechUserId())) {
            entity.setTechUserId(Arrays.stream(excelDto.getTechUserId().split(StringUtils.COMMA))
                    .map(it -> DictResolver.getItemValueByLabel(AdminDictEnum.sys_user, it)).collect(Collectors.joining(",")));
        }
        if(StringUtils.isNotBlank(excelDto.getOperateUserId())) {
            entity.setOperateUserId(Arrays.stream(excelDto.getOperateUserId().split(StringUtils.COMMA))
                    .map(it -> DictResolver.getItemValueByLabel(AdminDictEnum.sys_user, it)).collect(Collectors.joining(",")));
        }

        return entity;
    }

    @Override
    public RiskObjectExcelVO exportEntityToVo(RiskObjectEntity entity) {
        RiskObjectExcelVO excelVO = new RiskObjectExcelVO();
        BeanUtils.copyProperties(entity, excelVO);

        excelVO.setHazardType(DictResolver.getItemLabelByValue(RiskSysDictEnum.risk_object_type, entity.getHazardType()));
        if (ObjectUtil.isNotNull(entity.getReportDataFlag())) {
            excelVO.setReportDataFlag(StringUtils.getUseFlagStr(entity.getReportDataFlag()));
        }
        excelVO.setRiskLevel(DictResolver.getItemLabelByValue(RiskSysDictEnum.risk_level, entity.getRiskLevel()));
        excelVO.setDeptId(DictResolver.getItemLabelByValue(AdminDictEnum.sys_dept, entity.getDeptId()));
        excelVO.setPrincipal(DictResolver.getItemLabelByValue(AdminDictEnum.sys_user, entity.getPrincipal()));
        if (ObjectUtil.isNotNull(entity.getHazardSourceFlag())) {
            excelVO.setHazardSourceFlag(StringUtils.getUseFlagStr(entity.getHazardSourceFlag()));
        }
        excelVO.setHazardSourceLevel(DictResolver.getItemLabelByValue(RiskSysDictEnum.hazard_source_level, entity.getHazardSourceLevel()));
        excelVO.setControlLevel(DictResolver.getItemLabelByValue(RiskSysDictEnum.risk_control_level, entity.getControlLevel()));
        excelVO.setMainUserId(DictResolver.getItemLabelByValue(AdminDictEnum.sys_user, entity.getMainUserId()));
        excelVO.setTechUserId(DictResolver.getItemLabelByValue(AdminDictEnum.sys_user, entity.getTechUserId()));
        excelVO.setOperateUserId(DictResolver.getItemLabelByValue(AdminDictEnum.sys_user, entity.getOperateUserId()));

        return excelVO;
    }

    @Override
    public Map<Long, Long> countGroupByCompany() {
        return getBaseMapper().countGroupByCompany();
    }

    @Override
    public List<OrgLevelVO> listGroupLevelCompany() {
       return getBaseMapper().listGroupLevelCompany();
    }


}
